官方文档

routing 官方文档

*routing * 为路由

通过为每个文档指定自定义路由值,把文档索引到指定分片,未指定路由值时,默认 _id 为 routing

在父子文档下或祖孙文档下

a 为 b 的父文档,b 为 c 的父文档,c 为 a 的孙辈文档,这时 _routing 的定义如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
// 定义索引
PUT my_index
{
"mappings": {
"_doc": {
"properties": {
"my_join_field": {
"type": "join",
"relations": {
"a": ['b']
"b": "c"
}
}
}
}
}
}

// 插入 a 文档
PUT my_index/_doc/1?routing=space1&refresh
{
"text": "This is a vote",
"my_join_field":"a"
}

// 插入 b 文档
PUT my_index/_doc/2?routing=space1&refresh
{
"text": "This is a vote",
"my_join_field":{
"name": "b",
"parent": "1"
}
}

// 插入 c 文档
PUT my_index/_doc/3?routing=space1&refresh
{
"text": "This is a vote",
"my_join_field":{
"name": "c",
"parent": "2"
}
}

a、b、c 文档即都在 routing=space1 分片上,这时 a、b、c 之间的关系才成立,如果不在同一个 routing=space1 分片上,则关系不成立。